package com.baicaoyuan.web.controller;


import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.StringUtils;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.request.br.BRequirementIdRequest;
import com.baicaoyuan.web.request.br.BRequirementPageRequest;
import com.baicaoyuan.web.request.br.BRequirementPersonPageRequest;
import com.baicaoyuan.web.request.br.BRequirementRequest;
import com.baicaoyuan.web.response.RequirementPersonResponse;
import com.baicaoyuan.web.response.RequirementResponse;
import com.baicaoyuan.web.service.RequirementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "需求反馈Controller", tags = {"需求or反馈操作接口"})
@RestController
@RequestMapping(value = "/requirement")
public class RequirementController {


    @Autowired
    RequirementService requirementService;

    @ApiOperation(value = "发布需求（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/saveRequirement", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveRequirement(@RequestBody BRequirementRequest request) {
        try {
            requirementService.save(request, "1");
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "提交反馈（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/saveFeedBack", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveFeedBack(@RequestBody BRequirementRequest request) {
        try {
            requirementService.save(request, "2");
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "后台-删除需求or反馈（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/deleteRequirement", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status delete(@RequestBody BRequirementIdRequest request) {
        try {
            this.requirementService.delete(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-获取需求或者反馈列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getRequirements", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getRequirement(@RequestBody BRequirementPageRequest request) {
        try {
            List<RequirementResponse> user = this.requirementService.getRequirement(StringUtils.plusPercent(request.getTelephone()),
                    StringUtils.plusPercent(request.getCategory()), request.getPage(), request.getSize());
            Integer count = this.requirementService.getCountOfAll(StringUtils.plusPercent(request.getTelephone()), request.getCategory());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("requirements", user);
            result.setData("totalCount", count);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "获取个人需求或者反馈列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getPersonRequirements", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getPersonRequirements(@RequestBody BRequirementPersonPageRequest request) {
        try {
            List<RequirementPersonResponse> list = this.requirementService.getRequirementOrFeedback(request.getUserId(), request.getCategory(), request.getPage(), request.getSize());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("requirementfeedback", list);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台=-审核受理（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status check(@RequestBody BRequirementIdRequest request) {
        try {
            this.requirementService.check(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }
}
